import pygame, sys, math, random

pygame.init()

from Astroids import Astroid
from score import Score
from PowerUp import PowerUp
from PlayerBall import PlayerBall
from Enemy import Enemy
from HealthBar import *
from HealthBar2 import *

clock = pygame.time.Clock()

black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)

pygame.init()

pygame.display.set_caption('COMETS')

pygame.display.set_caption('My Game')

clock = pygame.time.Clock()

pygame.mixer.music.load('rsc/music/lazer.mp3')
pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
pygame.mixer.music.play(-1)



width = 700
height = 700
size = width, height

screen = pygame.display.set_mode(size)

bgImage = pygame.image.load("rsc/bg/gg.png")
bgRect = bgImage.get_rect()

bgColor = r,g,b = 0,0,0

player = PlayerBall(['rsc/player/player2.png'], [6,6], [50,50], [width/2,height/2])

player2 = PlayerBall(['rsc/player/sd.png'], [6,6], [50,50], [width/2,height/2])

healthbar = HB(player, (50,50))
healthbar2 = HB2(player2, (50 , height - 50))

asize = random.randint(25, 150)

astroids = [Astroid("rsc/enemy/ball.png", 

              [random.randint(-10,10), random.randint(-10,10)], 
              [asize, asize], 
              [random.randint(75, width-75), random.randint(75, height-75)])]

enemys = [Enemy("rsc/enemy/alien.png", 
              [random.randint(-10,10), random.randint(-10,10)], 
              [asize, asize], 
              [random.randint(75, width-75), random.randint(75, height-75)])]
              
powerUps = []
              
player.living = False

while True:
    while not player.living:
        score = 0
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    player.living = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_r:
                        player.living = True
                        player.health = 100
                        player2.health = 100
        pygame.mixer.music.load('rsc/music/lazer.mp3')
        pygame.mixer.music.play()
        screen.blit(bgImage, bgRect)
        pygame.display.flip()
        clock.tick(60)
    
    bgImage = pygame.image.load('rsc/bg/star.png')
    
    font = pygame.font.Font(None, 36)
    text = font.render('PLAYER 1', 1, green)
    text2 = font.render('PLAYER 2', 1, green)
    textpos = text.get_rect(centerx = 200)
    textpos2 = text2.get_rect(centerx = 200, centery = 600)
    pygame.display.update()
        
    level = 1
    powerUpTime = 600
    
    while player.living:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_d:
                    player.direction("right")
                if event.key == pygame.K_a:
                    player.direction("left")
                if event.key == pygame.K_w:
                    player.direction("up")
                if event.key == pygame.K_s:
                    player.direction("down")
                if event.key == pygame.K_RIGHT:
                    player2.direction("right")
                if event.key == pygame.K_LEFT:
                    player2.direction("left")
                if event.key ==  pygame.K_UP:
                    player2.direction("up")
                if event.key == pygame.K_DOWN:
                    player2.direction("down")
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_d:
                    player.direction("stop right")
                if event.key == pygame.K_a:
                    player.direction("stop left")
                if event.key == pygame.K_w:
                    player.direction("stop up")
                if event.key == pygame.K_s:
                    player.direction("stop down")
                if event.key == pygame.K_RIGHT:
                    player2.direction("stop right")
                if event.key == pygame.K_LEFT:
                    player2.direction("stop left")
                if event.key == pygame.K_UP:
                    player2.direction("stop up")
                if event.key == pygame.K_DOWN:
                    player2.direction("stop down")
        
        for astroid in astroids:
            astroid.update()
        for powerUp in powerUps:
            powerUp.update()
        for enemy in enemys:
            enemy.update()
        
        enemy.update()
        player.update()
        player2.update()
        healthbar.update()
        healthbar2.update()
        for astroid in astroids:
            astroid.collideWall(width, height)
        
        enemy.collideWall(width, height)
        player.collideWall(width, height)
        player2.collideWall(width, height)
        
        if len(astroids) > 1:
            for first in range(len(astroids)-1):
                for second in range(first+1,len(astroids)):
                    astroids[first].collideAstroid(astroids[second])
        
        for powerUp in powerUps:
            for astroid in astroids:
                powerUp.collideAstroid(astroid)
            for enemy in enemys:
                powerUp.collideEnemy(enemy)
                powerUp.collidePlayer(player)
                powerUp.collidePlayer(player2)
            if not powerUp.living:
                powerUps.remove(powerUp)
                
        for astroid in astroids:
            player.collideAstroid(astroid)
            player2.collideAstroid(astroid)
            enemy.collideAstroid(astroid)
            if not astroid.living:
                astroids.remove(astroid)
                score += 1
                
        for enemy in enemys:
            player.collideEnemy(enemy)
            player2.collideEnemy(enemy)
            healthbar.update()
            healthbar2.update()
        
        if len(astroids) == 0:
            level += 1
            for i in range(level):
                asize = random.randint(25, 150)
                astroids += [Astroid("rsc/enemy/ball.png", 
                                     [random.randint(-5,5), random.randint(-5,5)], 
                                     [asize, asize], 
                                     [random.randint(75, width-75), random.randint(75, height-75)])]
                              
        if powerUpTime >= 0:
            powerUpTime -= 1
        else:
            powerUpTime = 600
            powerUps += [PowerUp([random.randint(75, width-75), random.randint(75, height-75)])]
                          
        screen.blit(bgImage, bgRect)
        screen.blit(player.image, player.rect)
        screen.blit(player2.image, player2.rect)
        screen.blit(text, textpos)
        screen.blit(text2, textpos2)
        screen.blit(healthbar.image, healthbar.rect)
        screen.blit(healthbar2.image, healthbar2.rect)
        print str(score)
        for powerUp in powerUps:
            screen.blit(powerUp.image, powerUp.rect)
        for astroid in astroids:
            screen.blit(astroid.image, astroid.rect)
        for enemy in enemys:
            screen.blit(enemy.image, enemy.rect)
        pygame.display.flip()
        clock.tick(60)
        
        
        
    
        